<template>
  <div>
    <div class="search">
      <input v-model="keyword" class="search-input" type="text" placeholder="输入城市名或拼音">
    </div>
    <div class="search-content" ref="search" v-show="keyword">
      <ul>
        <li class="search-item border-bottom" v-for="(item,index) of list" :key="index" @click="hotcityclick(item.chinese)">
          {{item.chinese}}
        </li>
        <li class="search-item border-bottom" v-show="!list.length">
          没有找到匹配数据
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
  import Bscroll from "better-scroll";
import {mapState, mapMutations} from 'vuex'

  export default {
    props: ["citylists"],
    data() {
      return {
        keyword: "",
        timer: null,
        list: []
      };
    },
    methods: {
      hotcityclick(city) {
        console.log("2");
        this.changecity(city);
        this.$router.push({name: 'HelloWorld'});
        
      },
     ...mapMutations(['changecity'])
    },
    
    watch: {
      keyword() {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        if (!this.keyword) {
          this.list = [];
          return;
        }
        this.timer = setTimeout(() => {
          const result = [];
          for (let i in this.citylists.allCity) {
            this.citylists.allCity[i].cityList.forEach(element => {
              if (
                element.chinese.indexOf(this.keyword) > -1 ||
                element.spell.indexOf(this.keyword) > -1
              ) {
                result.push(element);
              }
            });
          }
          this.list = result;
        }, 100);
      }
    },
    mounted() {
      this.scroll = new Bscroll(this.$refs.search,{
        click:true
      });
    }

  };

</script>

<style scoped>
  .search {
    height: 0.72rem;
    background-color: #00bcd4;
    padding: 0 0.1rem;
  }

  .search-input {
    box-sizing: border-box;
    height: 0.62rem;
    line-height: 0.62rem;
    padding: 0 0.1rem;
    width: 100%;
    border-radius: 0.06rem;
    text-align: center;
    color: #666;
  }

  .search-content {
    position: absolute;
    z-index: 1;
    overflow: hidden;
    top: 1.58rem;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #eee;
  }

  .search-item {
    line-height: 0.62rem;
    padding-left: 0.2rem;
    background-color: #fff;
    color: #666;
  }

</style>
